﻿/**********************************天使物流*******************************************
* 功能描述:系统设置——操作员管理——分配角色
* 作者:孟帅龙
* 时间:2022/1/13
* 
**********************************天使物流*******************************************/
using Dapper;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.Common;
using TianShiProject.Models.System.ORRelations;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.System.ORRelations
{
    public class ORRelationRepository : DapperDbContextRepository, IORRelationRepository
    {
        private readonly IConfiguration _configuration;
        public ORRelationRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider, IConfiguration configuration) : base(dbContextProvider)
        {
            _configuration = configuration;
        }

        #region 操作员管理——分配角色——详情
        /// <summary>
        /// 操作员管理——分配角色——详情
        /// </summary>
        /// <param name="OperatorId"></param>
        /// <returns></returns>
        public async Task<List<ORRelationDto>> GetByORRelationId(string OperatorId)
        {
            return await this.QueryAsync<ORRelationDto>($"select Id,OperatorId,RoleId,IsDelete from ORRelation where OperatorId='{OperatorId}'");
        }
        #endregion

        #region 操作员管理——分配角色——添加
        /// <summary>
        /// 操作员管理——分配角色——添加
        /// </summary>
        /// <param name="OperatorId"></param>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public async Task<ResponseModel<bool>> PostAddORRelation(string OperatorId, string RoleId)
        {
            var result = new ResponseModel<bool>();
            using (IDbConnection conn = new SqlConnection(_configuration.GetConnectionString("Default")))
            {
                var arr = $"select Id,OperatorId,RoleId,IsDelete from ORRelation where OperatorId='{OperatorId}'";
                if (arr != null)
                {
                    await conn.ExecuteAsync($"delete from ORRelation where OperatorId='{OperatorId}'");
                }
                conn.Open();
                using (var begin = conn.BeginTransaction())
                {
                    try
                    {
                        foreach (var item in RoleId.Split(','))
                        {
                            await conn.ExecuteAsync($"insert into ORRelation values('{OperatorId}','{item}',0)", null, begin);
                        }
                        begin.Commit();
                        return result.Succeed(true);
                    }
                    catch (Exception)
                    {
                        begin.Rollback();
                        throw;
                    }
                }
            }
        }
        #endregion
    }
}
